import pages from '/pages.json'
// 获取当前页面路径保存到Storage 
const tabBarList = pages.tabBar.list
console.log('pages',tabBarList);
export function setCurrentUrl() {
	let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
	let curRoute = routes[routes.length - 1].route //获取当前页面路由
	let curParam = routes[routes.length - 1].options; //获取路由参数
	// 拼接参数
	let param = ''
	let i = 0
	for (let key in curParam) {
		if (i === 0) {
			param += '?' + key + '=' + curParam[key]
		} else {
			param += '&' + key + '=' + curParam[key]
		}
		i++
	}
	let fullPath = "/" + curRoute + param
	// 判断路径类型
	let type = isTabBar(fullPath)
	uni.setStorage({
		key: 'LOGIN-BACK-URL',
		data: {
			type: type,
			fullPath: fullPath
		}
	})
}
// 判断是否为tabBar
export function isTabBar(path) {
	let url;
	if(path.indexOf('/')===0){
		url = path.substring(1)
	}else{
		url = path
	}
	let type = "normal"
	if(tabBarList){
		tabBarList.forEach(item=>{
			if(url === item.pagePath){
				type = "tabbar"
				return
			}
		})
	}
	return type
}

// 路径跳转
export function redirectTo(path) {
	let type = isTabBar(fullPath)
	if (type === "tabbar") {
		uni.switchTab({
			url: path
		})
	} else {
		uni.redirectTo({
			url: path
		})
	}
}